package cn.dyj.middleware.dynamic.thread.pool.sdk.trigger.job;

import cn.dyj.middleware.dynamic.thread.pool.sdk.domain.IDynamicThreadPoolService;
import cn.dyj.middleware.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import cn.dyj.middleware.dynamic.thread.pool.sdk.registry.IRegistry;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * className:ThreadPoolDataReportJob
 * Description:线程池数据上报任务
 *
 * @Author 董雅洁
 * @Create 2025/3/4 15:48
 * @Version 1.0
 */
@Slf4j
public class ThreadPoolDataReportJob {
    //通过服务拿数据
    private final IDynamicThreadPoolService dynamicThreadPoolService;
    //通过redis注册中心上报
    private final IRegistry registry;

    public ThreadPoolDataReportJob(IDynamicThreadPoolService dynamicThreadPoolService,IRegistry registry){
        this.dynamicThreadPoolService=dynamicThreadPoolService;
        this.registry=registry;
    }


@Scheduled(cron = "0/20 * * * * ?")
    public void execReportThreadPoolList(){
        //通过service服务方法获得当前线程池的状态
        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolList();
        //向注册中心上报
        registry.reportThreadPool(threadPoolConfigEntities);
        log.info("动态线程池，上报线程池信息：{}", JSON.toJSONString(threadPoolConfigEntities));

        for (ThreadPoolConfigEntity threadPoolConfigEntity :
                threadPoolConfigEntities) {
            registry.reportThreadPoolConfigParameter(threadPoolConfigEntity);
            log.info("动态线程池，上报线程池配置：{}", JSON.toJSONString(threadPoolConfigEntity));
        }
    }
}
